//
//  HYOUSetUpView.m
//  BestGift
//
//  Created by 温晓宇 on 16/1/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HYOUSetUpView.h"

@interface HYOUSetUpView ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain) HYOUBaseTableView *myTableView;



@end

@implementation HYOUSetUpView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}
-(void)createSubViews{
    self.myTableView = [[HYOUBaseTableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self addSubview:self.myTableView];
    
    self.nightWitch = [[UISwitch alloc] init];
    [self.nightWitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.myTableView registerClass:[HYOUBaseTableViewCell class] forCellReuseIdentifier:@"cell"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 1;
    }else{
    return 2;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HYOUBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
#pragma mark -- 审核 夜间模式
        cell.accessoryView = self.nightWitch;
        cell.textLabel.text = @"夜间模式";
    }
    if (indexPath.section == 1) {
        cell.textLabel.text = @"清除缓存";
    }
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"分享应用";
        }else{
            cell.textLabel.text = @"关于我们";
        }
//        cell.accessoryView
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
//            NSString *states = [[NSUserDefaults standardUserDefaults] objectForKey:@"night"];
//            if ([states isEqualToString:@"yes"]) {
//                //黑夜状态
//                NSLog(@"daf");
//                [[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:@"night"];
//                //发送通知
//                [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:[UIColor whiteColor]];
//                
//            }else
//            {
//                [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"night"];
//                //发送通知
//                [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:NIGHT_COLOR];
//                
//                
//            }
        }
    }
    if (indexPath.section == 1) {
        NSLog(@"清除缓存");
        [self.delegate clear];
    }
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            [self.delegate UMSocial];
            
        }else{
            [self.delegate showMyView];
        }
    }
}
-(void)switchAction:(UISwitch *)sw{
    NSString *states = [[NSUserDefaults standardUserDefaults] objectForKey:@"night"];
    if ([states isEqualToString:@"yes"]) {
        //黑夜状态

        [[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:@"night"];
        //发送通知
#pragma mark -- 审核 通知中心
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:[UIColor whiteColor]];
        
    }else
    {
        [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"night"];
        //发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:NIGHT_COLOR];
        
        
    }
}
@end
